package com.course.designpatterns.singleton;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/6/20 20:53
 * @description：懒汉式 有选择的加同步锁
 * 线程不安全
 */
public class Mgr05
{
    private static Mgr05 INSTANCE;

    private Mgr05()
    {
    }

    ;

    public static Mgr05 getInstance()
    {
        if (INSTANCE == null)
        {
            synchronized (Mgr05.class)
            {
                try
                {
                    Thread.sleep(20);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                INSTANCE = new Mgr05();
            }
        }

        return INSTANCE;
    }

    public static void main(String[] args)
    {

//线程不安全
        for (int i = 0; i < 100; i++)
        {
            new Thread(() ->
            {
                System.out.println(getInstance().hashCode());
            }).start();
        }

    }


}
